package fastjson.code;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * Description: JSONObject的使用
 *
 * @author code-worker
 * @date 2021/08/18 22:32
 */
public class JsonObjectDemo {
    private String demoName;
    private String demoDesc;
    private JsonDemo jsonDemo;
    JsonObjectDemo(){}
    private JsonObjectDemo(String demoName, String demoDesc, JsonDemo jsonDemo) {
        this.demoName = demoName;
        this.demoDesc = demoDesc;
        this.jsonDemo = jsonDemo;
    }
    public JsonDemo getJsonDemo() {
        return jsonDemo;
    }
    public void setJsonDemo(JsonDemo jsonDemo) {
        this.jsonDemo = jsonDemo;
    }
    public String getDemoName() {
        return demoName;
    }
    public void setDemoName(String demoName) {
        this.demoName = demoName;
    }
    public String getDemoDesc() {
        return demoDesc;
    }
    public void setDemoDesc(String demoDesc) {
        this.demoDesc = demoDesc;
    }

    @Override
    public String toString() {
        return "JsonObjectDemo{" +
                "demoName='" + demoName + '\'' +
                ", demoDesc='" + demoDesc + '\'' +
                '}';
    }


    public static void main(String[] args) {
        JsonDemo JSONDemo = new JsonDemo("demo-demo", "示例的示例");
        String str = JSON.toJSONString(new JsonObjectDemo("demo", "演示示例", JSONDemo));
        JSONObject jsonDemo = JSON.parseObject(str);
        System.out.println("jsonDemo:"+jsonDemo);
        System.out.println("isEmpty: "+ jsonDemo.isEmpty());
        System.out.println("size: "+ jsonDemo.size());
        System.out.println("containKey: "+ jsonDemo.containsKey("demoName"));
        System.out.println("containValue: "+ jsonDemo.containsValue("demo"));
        System.out.println("containKey: "+ jsonDemo.containsValue("demo-demo"));
        System.out.println("containKey: "+ jsonDemo.containsValue(new JsonDemo("demo-demo", "示例的示例")));
        System.out.println("containKey: "+ jsonDemo.containsValue(JSONDemo));
        //需要有对应的getter、setter方法才可以使用getObject方法
        System.out.println("getObject: "+ jsonDemo.getObject("JsonDemo", JsonDemo.class));
        System.out.println("jsonDemo put: "+jsonDemo.put("demo", JSONDemo));
        System.out.println("jsonDemo put: "+jsonDemo);
        System.out.println("jsonDemo fluentPut: "+ jsonDemo.fluentPut("demo2", JSONDemo));
        System.out.println("jsonDemo fluentPut: "+ jsonDemo);
        System.out.println("jsonDemo fluentPut: "+ jsonDemo.getJSONObject("demo2"));
        System.out.println("jsonDemo remove: "+jsonDemo.remove("demo"));
        System.out.println("jsonDemo remove: "+jsonDemo);
        System.out.println("jsonDemo fluentRemove: "+jsonDemo.fluentRemove("demo2"));
        System.out.println("jsonDemo fluentRemove: "+jsonDemo);
        System.out.println(jsonDemo.put("demoName", "hello"));
        System.out.println(jsonDemo);
        System.out.println("keySet: "+jsonDemo.keySet());
        for (String s : jsonDemo.keySet()){
            System.out.println("遍历JSONObject: key="+s+", value="+jsonDemo.get(s));
        }
        System.out.println("values: "+jsonDemo.values());
        System.out.println("entrySet: "+jsonDemo.entrySet());
    }
}
